package com.xiaohua.api.common.properties;

import com.xiaohua.api.domain.XhApiCredentials;
import com.xiaohua.api.service.IXhApiCredentialsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 阿里云配置属性
 *
 * @author 小花
 * @date 2025-03-29
 */
@Component
@ConfigurationProperties(prefix = "aliyun")
public class AliYunProperties {
    
    private static final Logger log = LoggerFactory.getLogger(AliYunProperties.class);
    
    private static final String ALIYUN_API_NAME = "Aliyun_Api";

    /**
     * AccessKey ID
     */
    private String accessKeyId;

    /**
     * AccessKey Secret
     */
    private String accessKeySecret;

    /**
     * OSS Bucket
     */
    private String ossBucket;

    /**
     * OSS Endpoint
     */
    private String ossEndpoint;
    
    @Autowired
    private IXhApiCredentialsService apiCredentialsService;
    
    /**
     * 初始化方法，从数据库加载阿里云凭证
     */
    @PostConstruct
    public void init() {
        loadCredentialsFromDatabase();
    }
    
    /**
     * 从数据库加载凭证
     */
    public void loadCredentialsFromDatabase() {
        try {
            XhApiCredentials query = new XhApiCredentials();
            query.setApiName(ALIYUN_API_NAME);
            List<XhApiCredentials> list = apiCredentialsService.selectXhApiCredentialsList(query);
            
            if (list != null && !list.isEmpty()) {
                XhApiCredentials credential = list.get(0);
                log.info("从数据库加载阿里云API凭证");
                this.accessKeyId = credential.getApiKey();
                this.accessKeySecret = credential.getApiSecret();
                log.info("凭证加载成功，AK: {}", maskString(this.accessKeyId));
            } else {
                log.warn("数据库中未找到阿里云API凭证，将使用配置文件中的凭证");
            }
        } catch (Exception e) {
            log.error("从数据库加载阿里云API凭证失败: {}", e.getMessage(), e);
        }
    }

    /**
     * 获取AccessKey ID，每次获取都检查是否需要从数据库重新加载
     */
    public String getAccessKeyId() {
        refreshCredentialsIfNeeded();
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    /**
     * 获取AccessKey Secret，每次获取都检查是否需要从数据库重新加载
     */
    public String getAccessKeySecret() {
        refreshCredentialsIfNeeded();
        return accessKeySecret;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }
    
    /**
     * 如果凭证为空，刷新从数据库加载
     */
    private void refreshCredentialsIfNeeded() {
        if (accessKeyId == null || accessKeySecret == null) {
            log.info("检测到凭证为空，从数据库重新加载");
            loadCredentialsFromDatabase();
        }
    }

    public String getOssBucket() {
        return ossBucket;
    }

    public void setOssBucket(String ossBucket) {
        this.ossBucket = ossBucket;
    }

    public String getOssEndpoint() {
        return ossEndpoint;
    }

    public void setOssEndpoint(String ossEndpoint) {
        this.ossEndpoint = ossEndpoint;
    }
    
    /**
     * 对敏感字符串进行掩码处理
     */
    private String maskString(String input) {
        if (input == null || input.length() <= 4) {
            return "***";
        }
        return input.substring(0, 4) + "**********";
    }
} 